package at.btd.bsk.gps;

import java.util.List;
import java.util.Vector;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.Log;
import at.btd.bsk.entities.Trip;
import at.btd.bsk.entities.Waypoint;

import com.google.android.maps.MapView;

/**
 * Displays an Overlay over the Map showing the Current selected Route and the
 * current travelled Route
 * 
 * @author Matthias
 * 
 */
public class TripOverlay extends com.google.android.maps.Overlay {
	private Trip trip = null;
	private List<Waypoint> waypoints = new Vector<Waypoint>();
	private int[] colors = new int[40];

	public TripOverlay(Trip trip, MapView mv) {
		this.trip = trip;

		updateGeoPoints();
			
		// starts by Green and goes to Yellow and then to red
		colors[0] = Color.rgb(0,207,5);
		colors[1] = Color.rgb(0,207,5);
		colors[2] = Color.rgb(0,207,5);
		colors[3] = Color.rgb(0,207,5);
		colors[4] = Color.rgb(0,207,5);
		colors[5] = Color.rgb(0,207,5);
		colors[6] = Color.rgb(3,207,5);
		colors[7] = Color.rgb(14,211,5);
		colors[8] = Color.rgb(29,214,5);
		colors[9] = Color.rgb(46,220,4);
		colors[10] = Color.rgb(65,226,4);
		colors[11] = Color.rgb(86,232,5);
		colors[12] = Color.rgb(108,239,5);
		colors[13] = Color.rgb(130,245,4);
		colors[14] = Color.rgb(152,250,4);
		colors[15] = Color.rgb(173,250,4);
		colors[16] = Color.rgb(193,250,4);
		colors[17] = Color.rgb(212,250,3);
		colors[18] = Color.rgb(228,250,3);
		colors[19] = Color.rgb(253,250,3); 
		colors[20] = Color.rgb(255,230,3);
		colors[21] = Color.rgb(255,200,2);
		colors[22] = Color.rgb(255,162,2);
		colors[23] = Color.rgb(255,121,1);
		colors[24] = Color.rgb(255,80,1);
		colors[25] = Color.rgb(255,61,1);
		colors[26] = Color.rgb(255,43,0);
		colors[27] = Color.rgb(255,28,0);
		colors[28] = Color.rgb(255,12,0);
		colors[29] = Color.rgb(255,3,0);
		colors[30] = Color.rgb(255,0,0);
		colors[31] = Color.rgb(255,0,0);
		colors[32] = Color.rgb(255,0,0);
		colors[33] = Color.rgb(255,0,0);
		colors[34] = Color.rgb(255,0,0);
		colors[35] = Color.rgb(255,0,0);
		colors[36] = Color.rgb(255,0,0);
		colors[37] = Color.rgb(255,0,0);
		colors[38] = Color.rgb(255,0,0);   
		colors[39] = Color.rgb(255,0,0);
		
	}

	private void updateGeoPoints() {
		if (this.trip != null && this.trip.getWaypoints() != null) {
			this.waypoints.clear();

			for (Waypoint p : this.trip.getWaypoints()) {
				this.waypoints.add(p);
			}
		}
	}

	@Override
	public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
		super.draw(canvas, mv, shadow);

		updateGeoPoints();
		drawPath(mv, canvas, this.waypoints, false);

		return true;
	}

	public void drawPath(MapView mv, Canvas canvas, List<Waypoint> points, boolean dotted) {
		if (points != null && !points.isEmpty()) {
			Paint paint = new Paint();

			paint.setStyle(Paint.Style.STROKE);
			
			if (dotted) {
				paint.setPathEffect(new DashPathEffect(new float[] {5,5}, 1));
			}

			
			// set width depending on zoom level
			if (mv.getZoomLevel() < 13) {
				paint.setStrokeWidth(3);
			} else if (mv.getZoomLevel() == 13) {
				paint.setStrokeWidth(5);
			} else if (mv.getZoomLevel() == 14) {
				paint.setStrokeWidth(7);
			} else if (mv.getZoomLevel() == 15) {
				paint.setStrokeWidth(9);
			} else if (mv.getZoomLevel() == 16) {
				paint.setStrokeWidth(11);
			} else if (mv.getZoomLevel() == 17) {
				paint.setStrokeWidth(13);
			} else {
				paint.setStrokeWidth(15);
			}
			
			if (dotted) {
				paint.setStrokeWidth(paint.getStrokeWidth() - 1);
			}

			paint.setStrokeCap(Paint.Cap.ROUND);
			paint.setStrokeJoin(Paint.Join.ROUND);

			Point lastPoint = new Point();
			
			for (int i = 0; i < points.size(); i++) {
				Point point = new Point();
				int velocity = Math.max(0,Math.min(Math.round(points.get(i).getSpeed()*3.6f), 40) - 1);
				
				Log.i(getClass().getName(), "Calculated color index: " + velocity);
				

				mv.getProjection().toPixels(points.get(i).getGeoPoint(), point);
				
				// set color according to speed
				paint.setColor(this.colors[velocity]);
				
				if (i > 0) {
				    canvas.drawLine(lastPoint.x, lastPoint.y, point.x, point.y, paint);
				}
				
				lastPoint = point;
			}
		}
	}
}